/**
 * 
 */
package org.gnf.genewiki.associations.analysis;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.gnf.genewiki.Config;
import org.gnf.genewiki.associations.CandiAnnoSorter;
import org.gnf.genewiki.associations.CandidateAnnotation;
import org.gnf.genewiki.associations.CandidateAnnotations;

/**
 * Manage the data used for the manual review of a sample of predicted annotations
 * @author bgood
 *
 */
public class ManualEvaluation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
//		List<CandidateAnnotation> cannos = generateRandomSampleOfDOcannos(10, Config.text_mined_annos);
//		CandidateAnnotations c = new CandidateAnnotations();
//		c.setCannos(cannos);
//		c.writeCandiListWithEvidence(Config.gwroot+"/output/do_eval_sample.txt");
		
		List<CandidateAnnotation> cannos = generateRandomSampleOfGOcannos(200, Config.text_mined_annos);
		CandidateAnnotations c = new CandidateAnnotations();
		c.setCannos(cannos);
		c.writeGOCandiListWithEvidence(Config.gwroot+"/output/go_eval_sample.txt");
	}

	/**
	 * Pull out a random selection 'max' annotations
	 * @param max
	 * @return
	 */
	public static List<CandidateAnnotation> generateRandomSampleOfDOcannos(int percent, String anno_file){
		CandidateAnnotations cannolist = new CandidateAnnotations();
		cannolist.loadAndFilterCandidateDOAnnotations(anno_file);
		List<CandidateAnnotation> cannos = cannolist.getCannos();
		System.out.println("Loaded candidate DO annotations "+cannos.size());
		cannos = CandiAnnoSorter.addAllEvidenceForDOAnnotations(cannos);
		//remove unintended order
		Collections.shuffle(cannos);
		float max = 0; //get percent% of the potential new ones
		float total_cand = 0;
		for(CandidateAnnotation canno : cannos){
			if(!canno.getLink_author().equals("ProteinBoxBot")&&
					!canno.getEvidence().isGoObsolete()&&
					!canno.getEvidence().isMatches_existing_annotation_directly()&&
					!canno.getEvidence().isMatches_parent_of_existing_annotation()){
				total_cand++;
			}
		}
		max = total_cand/percent;
		System.out.println(max+"n to sample of total candidates:\t"+total_cand);

		List<CandidateAnnotation> rannos = new ArrayList<CandidateAnnotation>();
		int n = 0; int n_summary = 0;
		for(CandidateAnnotation canno : cannos){
			if(n<max&&
					!canno.getLink_author().equals("ProteinBoxBot")&&
					!canno.getEvidence().isGoObsolete()&&
					!canno.getEvidence().isMatches_existing_annotation_directly()&&
					!canno.getEvidence().isMatches_parent_of_existing_annotation()){
				rannos.add(canno);
				n++;
				//for sanity check on distributions
				if(canno.getSection_heading().equals("Summary")){
					n_summary++;
				}
			}
		}
		System.out.println(n+" "+n_summary+"\t"+(float)n_summary/(float)n+"\tshould be like 39%");
		return rannos;
	}


	/**
	 * Pull out a random selection 'max' annotations
	 * @param max
	 * @return
	 */
	public static List<CandidateAnnotation> generateRandomSampleOfGOcannos(int max, String anno_file){
		CandidateAnnotations cannolist = new CandidateAnnotations();
		cannolist.loadAndFilterCandidateGOAnnotations(anno_file);
		List<CandidateAnnotation> cannos = cannolist.getCannos();
		System.out.println("Loaded candidate GO annotations "+cannos.size());
		cannos = CandiAnnoSorter.addAllEvidenceForGOAnnotations(cannos);
		//remove unintended order
		Collections.shuffle(cannos);
		List<CandidateAnnotation> rannos = new ArrayList<CandidateAnnotation>();
		//
		float n = 0;
		float n_summary = 0;
		for(CandidateAnnotation canno : cannos){
			if(n<max&&
					!canno.getLink_author().equals("ProteinBoxBot")&&
					!canno.getEvidence().isGoObsolete()&&
					!canno.getEvidence().isMatches_existing_annotation_directly()&&
					!canno.getEvidence().isMatches_parent_of_existing_annotation()&&
					canno.getVocabulary_branch().equals("biological_process")){ 
				rannos.add(canno);
				n++;
				//for sanity check on distributions
				//for sanity check on distributions
				if(canno.getSection_heading().equals("Summary")){
					n_summary++;
				}
			}
		}
		System.out.println(n+" "+n_summary+"\t"+n_summary/n+"\tshould be like 45%");
		return rannos;
	}

	/**
	 * Build a random sample to show we are doing better than random	
	 * @param max
	 * @return
	 */
	public static Set<String> generateNegativeControl(int max, String anno_file) {
		//get all the candidates to start with
		List<CandidateAnnotation> cannos = generateRandomSampleOfGOcannos(10000000, anno_file);
		//get the genes and ont terms
		Set<String> genes = new HashSet<String>();
		Set<String> terms = new HashSet<String>();
		for(CandidateAnnotation canno : cannos){
			genes.add(canno.getEntrez_gene_id());
			terms.add(canno.getTarget_accession()+"\t"+canno.getTarget_preferred_term());
		}
		List<String> glist = new ArrayList<String>(genes);
		List<String> tlist = new ArrayList<String>(terms);
		Collections.shuffle(glist); Collections.shuffle(tlist);
		Set<String> rannos = new HashSet<String>();
		System.out.println("genes "+glist.size()+" terms "+tlist.size());
		while(rannos.size()<max){
			String r = glist.get((int) Math.round(Math.random()*glist.size()))+"\t"+
			tlist.get((int) Math.round(Math.random()*tlist.size()));
			rannos.add(r);
			System.out.println(r);
		}
		return rannos;
	}


}
